package xing.appwidget.main

import android.annotation.SuppressLint
import android.app.Application
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.util.Log
import androidx.annotation.WorkerThread
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers
import xing.appwidget.bean.ApplicationItem
import xing.appwidget.bean.PackageFilterParam
import xing.appwidget.utils.Utils
import java.util.concurrent.TimeUnit

class MainViewModel(application: Application) : AndroidViewModel(application) {
    companion object {
        private const val TAG = "MainViewModel"
    }

    private val applicationItemsLD = MutableLiveData<List<ApplicationItem>>()
    private val applicationFilterLD = MutableLiveData<PackageFilterParam>()

    private val applicationItems = ArrayList<ApplicationItem>()
    var appFilter = PackageFilterParam(system = false)

    private val packageManager: PackageManager by lazy { (getApplication() as Application).packageManager }

    fun observeAppList(activity: LifecycleOwner, observer: Observer<List<ApplicationItem>>) {
        applicationItemsLD.observe(activity, observer)
    }

    fun observeAppFilter(activity: LifecycleOwner, observer: Observer<PackageFilterParam>) {
        applicationFilterLD.observe(activity, observer)
    }

    fun loadAppFilterSetting() {
//        Observable.just(PackageFilterParam.get())
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe { applicationFilterLD.postValue(it) }
    }

    fun loadApplicationItems() {
        Observable.just(doLoadApplicationItems())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ applicationItemsLD.postValue(it) },
                        { e -> Log.e(TAG, "loadApplicationItems: ", e) })
    }

    @WorkerThread
    @SuppressLint("PackageManagerGetSignatures")
    private fun doLoadApplicationItems(): List<ApplicationItem> {
        applicationItems.clear()
        @SuppressLint("WrongConstant") val packageInfoList: List<PackageInfo> =
                packageManager.getInstalledPackages(
//                        PackageUtils.flagSigningInfo or
                        PackageManager.GET_ACTIVITIES
//                              or  PackageUtils.flagDisabledComponents
                )
        for (packageInfo in packageInfoList) {
            if (appFilter.isValid(packageInfo)) {
                applicationItems.add(Utils.toApplicationItem(packageManager, packageInfo))
            }
        }
//        sortApplicationList(mSortBy)
        applicationFilterLD.postValue(appFilter)
        return applicationItems
    }

}